package com.oig.sys.msg.runner;

import com.oig.sys.msg.app.IAppMqMsgService;
import com.oig.sys.msg.app.IAppNoticeMsgService;
import com.oig.sys.msg.service.ITbMqConfigService;
import com.oig.sys.msg.thread.MqMsgPublisher;
import com.oig.sys.msg.thread.MsgPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class PusherStarter implements InitializingBean, DisposableBean {


    private final IAppNoticeMsgService appNoticeMsgService;
    private final IAppMqMsgService appMqMsgService;
    private final ITbMqConfigService mqConfigService;
    private final RabbitTemplate rabbitTemplate;

    public PusherStarter(IAppNoticeMsgService appNoticeMsgService,
                         IAppMqMsgService appMqMsgService,
                         ITbMqConfigService mqConfigService,
                         RabbitTemplate rabbitTemplate){
        this.appNoticeMsgService = appNoticeMsgService;
        this.appMqMsgService = appMqMsgService;
        this.mqConfigService = mqConfigService;
        this.rabbitTemplate = rabbitTemplate;
    }


    private MsgPublisher msgPublisher;
    private MqMsgPublisher mqMsgPublisher;


    /**
     * 初始化这个bean后启动各种推送轮询线程
     *
     */
    @Override
    public void afterPropertiesSet() {
        log.info("线程启动");
        msgPublisher = new MsgPublisher(appNoticeMsgService);
        msgPublisher.start();
        //mq固定轮询扫描表未发送表，如果多副时，可能重复发送
        mqMsgPublisher = new MqMsgPublisher(appMqMsgService, mqConfigService, rabbitTemplate);
        mqMsgPublisher.start();
    }



    @Override
    public void destroy() {
        msgPublisher.toStop();
        mqMsgPublisher.toStop();
    }

}
